package com.wude.guild.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.wude.bean.ReceiveBean;
import com.wude.bean.TrafficListBean;
import com.wude.bean.UserInfo;
import com.wude.guild.R;
import com.wude.guild.face.TrafficAdapterFace;
import com.wude.http.HttpCom;
import com.wude.tools.DbUtils;
import com.wude.tools.Utils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 描述：领取流量Holder
 * 时间: 2017-09-04 9:32
 */
public class TrafficHolder extends BaseHolder<TrafficListBean.DataBean> {
    private Activity activity;
    private TrafficListBean.DataBean data;

    @BindView(R.id.tv_Time)
    TextView tv_Time;//时间--今天、10月25日
    @BindView(R.id.tv_liuliang)
    TextView tv_liuliang;//领取的流量大小--1M
    @BindView(R.id.tv_type)
    TextView tv_type;//领取状态--领取、已领取、已失效
    @BindView(R.id.topLine)
    View topLine;
    @BindView(R.id.bottomLine)
    View bottomLine;
    @BindView(R.id.iv_yuan)
    ImageView iv_yuan;
    @BindView(R.id.ll_content)
    LinearLayout ll_content;
    private TrafficAdapterFace face;

    private int count;

    public TrafficHolder(TrafficAdapterFace face) {
        this.face = face;
    }

    @Override
    protected void refreshView(TrafficListBean.DataBean dataBean, int position, Activity activity) {
        this.activity = activity;
        this.data = dataBean;

        if (dataBean.isHasMore() && position == 0) {//不止一条数据
            topLine.setVisibility(View.INVISIBLE);
            bottomLine.setVisibility(View.VISIBLE);
        }

        if (!dataBean.isHasMore() && position == 0) {//只有一条数据
            topLine.setVisibility(View.INVISIBLE);
            bottomLine.setVisibility(View.INVISIBLE);
        }

        if (position == 0) {
            count = dataBean.getCount();
        }

        if (position != 0 && !dataBean.isHasMore()) {
            bottomLine.setVisibility(View.INVISIBLE);
        }

        if (position % 2 == 0) {
            ll_content.setBackgroundResource(R.color.white);
        } else if (position % 2 == 1) {
            ll_content.setBackgroundResource(R.color.f8f8f8);
        }

        tv_Time.setText(dataBean.getCreate_time());
        tv_liuliang.setText(dataBean.getFlow() + "M");

        String status = dataBean.getStatus();
        int c4 = activity.getResources().getColor(R.color.font_c4c4c4);
        int black = activity.getResources().getColor(R.color.black);

        switch (status) {
            case "0":
                tv_type.setText("已失效");
                tv_type.setTextColor(c4);
                tv_Time.setTextColor(c4);
                tv_liuliang.setTextColor(c4);
                break;
            case "1":
                tv_type.setText("已领取");
                tv_type.setTextColor(c4);
                tv_Time.setTextColor(c4);
                tv_liuliang.setTextColor(c4);
                break;
            case "2":
                tv_type.setText("领取");
                tv_type.setTextColor(activity.getResources().getColor(R.color.white));
                tv_type.setBackgroundResource(R.drawable.blue_corners_bg_5);
                tv_Time.setTextColor(black);
                tv_liuliang.setTextColor(black);
                break;
        }
    }

    @Override
    protected View initView() {
        View inflate = LinearLayout.inflate(x.app(), R.layout.holder_liulianglingqu, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        return inflate;
    }

    @OnClick({R.id.tv_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_type:
                String status = data.getStatus();
                if ("2".equals(status)) {
                    getTraffic();
                } else {
                    tv_type.setClickable(false);
                }
                break;
        }
    }


    /**
     * 领取流量
     */
    private void getTraffic() {
        tv_type.setClickable(false);
        UserInfo info = Utils.getLoginUser();
        Map<String, String> map = new HashMap<>();
        map.put("token", info.token);
        HttpCom.POST(mHandler, HttpCom.getGetFlow(), map, false);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        Log.e("领取流量返回的数据", msg.obj.toString());
                        //领取流量返回结果处理
                        ReceiveBean bean = new Gson().fromJson(msg.obj.toString(), ReceiveBean.class);
                        if (bean.getStatus() == 1) {
                            updateUserInfo(bean.getData()); //领取成功时更新用户信息
                            ToastUtils.showShortToast(bean.getReturn_code());
//                            new GetTrafficSuccessDialog(activity, R.style.MillionDialogStyle).show();
                            int c4 = activity.getResources().getColor(R.color.font_c4c4c4);
                            tv_type.setBackgroundResource(R.color.transparency);
                            tv_type.setText("已领取");
                            tv_type.setTextColor(c4);
                            tv_Time.setTextColor(c4);
                            tv_liuliang.setTextColor(c4);
                            tv_type.setClickable(false);
                        } else {
                            ToastUtils.showShortToast(bean.getReturn_code());
                            tv_type.setClickable(true);
                        }
                    } catch (Exception e) {
                        ToastUtils.showShortToast("数据解析异常");
                        Log.e("领取流量异常：", e.toString());
                    }
                    break;
                case 2:
                    ToastUtils.showShortToast("网络错误");
                    break;
            }
        }
    };

    private void updateUserInfo(UserInfo userInfo) {
        if (userInfo != null) {
            Log.e("用户已领取的流量共(M)：", userInfo.all_flow);
            try {
                DbManager db = DbUtils.getDB();
                db.saveOrUpdate(userInfo);
            } catch (DbException e) {
                e.printStackTrace();
            }
            if (face != null)
                face.getSuccess(userInfo);
        }
    }
}
